//
//  GainViewController.swift
//  DSP
//
//  Created by tucici on 2024/5/14.
//

import UIKit
import SwiftyJSON
import TCCKit
private enum CellType{
    case top(_ array:[ChannelModel])
    case mid
    case bottom(_ array:[GroupModel])
}
class GainViewController: DSBaseController {
    private var array = [CellType]()
    override func viewDidLoad() {
        super.viewDidLoad()
        initUI()
    }
    
    override func initUI() {
        tableView.delegate = self
        tableView.dataSource = self
        tableView.isScrollEnabled = false
        tableView.register(GainTopCell.self, forCellReuseIdentifier: GainTopCell.ID())
        tableView.register(GainMidCell.nib(), forCellReuseIdentifier: GainMidCell.ID())
        tableView.register(GroupCell.self, forCellReuseIdentifier: GroupCell.ID())
        tableView.snp.makeConstraints { make in
            make.left.right.top.equalToSuperview()
            make.bottom.equalToSuperview().inset(KUITabBarHeight)
        }
        
    }
    
    @objc override func updateData() {
        super.updateData()
        array = [.top(APP.CHM.array),
                 .mid,
                 .bottom(groupArray)]
        tableView.reloadData()
    }
    
    override func sendData(){
        let ch_array = APP.CHM.array.filter({$0.select})
        if ch_array.isEmpty {return}
        for ch in ch_array{
            _ = BLEData.mute(ch).write
            _ = BLEData.phase(ch).write
        }
    }
    
    ///同一group的通道的属性
    private func copyProperties(from ch:ChannelModel){
        guard let group = groupArray.first(where: {$0.contain(ch: ch)}) else {return}
        select_group_index = group.select ? group.num : 100
        if group.select == false || ch.select == false {return}
        for ch_t in group.chArr{
            if ch_t.num == ch.num {continue}
            ch_t.select = group.select
            ch_t.Volume = ch.Volume
            ch_t.mute = ch.mute
            ch_t.phase = ch.phase
        }
    }
    
}

//MARK: - GainTopCellDelegate
extension  GainViewController : GainTopCellDelegate{
    func swipe(ch: ChannelModel, index: Int) {
        copyProperties(from: ch)
        sendData()
    }
    
    func select(ch: ChannelModel, index: Int) {
        let group_select = groupArray.first(where: {$0.contain(ch: ch)  && $0.select}) ?? GroupModel()
        _ = APP.CHM.array.map{
            $0.select = (group_select.contain(ch: $0)) ? group_select.select : $0.num == ch.num
        }
        copyProperties(from: ch)
        select_ch_index = index
        updateData()
        sendData()
    }
}


//MARK: - GroupCellDelegate
extension GainViewController : GroupCellDelegate{
    func select(_ group: GroupModel, _ index: Int) {
        select_ch_index = group.select ? group.ch1.num - 1 : select_ch_index
        copyProperties(from: group.ch1)
        updateData()
        sendData()
    }
}

//MARK: - UITableViewDataSource
extension GainViewController:UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return array.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        switch array[indexPath.row]{
        case let .top(array):
            let cell = tableView.dequeueReusableCell(withIdentifier: GainTopCell.ID()) as! GainTopCell
            cell.array = array
            cell.delegate = self
            return cell
        case .mid:
            let cell = tableView.dequeueReusableCell(withIdentifier: GainMidCell.ID()) as! GainMidCell
            cell.updateUI()
            return cell
        case let .bottom(array):
            let cell = tableView.dequeueReusableCell(withIdentifier: GroupCell.ID()) as! GroupCell
            cell.ignoredEnable = true
            cell.array = array
            cell.delegate = self
            return cell
        }
    }
}
//MARK: - UITableViewDelegate
extension GainViewController:UITableViewDelegate{
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return chartView.bounds.height
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.01
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view = UIView()
        view.backgroundColor = .clear
        view.addSubview(chartView)
        chartView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        return view
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return nil
    }
}
